package com.quanquan.management.controller;

import DTO.PageDTO;
import com.quanquan.management.dto.UserDTO;
import com.quanquan.management.dto.UserGroupDTO;
import com.quanquan.management.request.UserGroupRequestBody;
import com.quanquan.management.service.UserGroupService;
import enums.ResCodeEnum;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import utils.StringUtil;
import web.response.ResultBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Log4j2
@Controller
@RequestMapping("/user_group")
public class UserGroupController extends ManagerBaseController {

    @Autowired
    private UserGroupService userGroupService;

    /**
     * 添加分组
     *
     * @param requestBody
     * @return
     */
    @PostMapping("/add")
    public @ResponseBody
    ResultBody add(UserGroupRequestBody requestBody) {
        String groupName = requestBody.getGroupName();
        boolean exists = userGroupService.exists(groupName);
        if (exists) {
            return new ResultBody(ResCodeEnum.FAIL.getCode(), "分组已存在");
        }
        boolean add = userGroupService.create(requestBody);
        if (add) {
            return new ResultBody(ResCodeEnum.SUCCESS.getCode(), ResCodeEnum.SUCCESS.getMsg());
        }
        return new ResultBody(ResCodeEnum.FAIL.getCode(), ResCodeEnum.FAIL.getMsg());
    }

    /**
     * 条件查询列表
     *
     * @param pageSize
     * @param page
     * @param keyword
     * @param sort
     * @param fromDate
     * @param toDate
     * @param model
     * @return
     */
    @RequestMapping("/list")
    public String list(@RequestParam(required = false) Integer pageSize,
                       @RequestParam(required = false) Integer page,
                       @RequestParam(required = false) String keyword,
                       @RequestParam(required = false) String sort,
                       @RequestParam(required = false) String fromDate,
                       @RequestParam(required = false) String toDate,
                       Model model) {
        if (null == pageSize) {
            pageSize = 10;
        }
        if (null == page) {
            page = 0;
        } else if (page > 0) {
            page = page - 1;
        }
        UserGroupRequestBody requestBody = new UserGroupRequestBody();
        requestBody.setSort(sort);
        requestBody.setKeyword(keyword);
        requestBody.setPage(page);
        requestBody.setSize(pageSize);
        requestBody.setToDate(toDate);
        requestBody.setFromDate(fromDate);
        PageDTO<UserGroupDTO> pageDTO = userGroupService.list(requestBody);
        model.addAttribute("pageObj", pageDTO);
        return "userGroup/list";
    }

    /**
     * 删除分组
     *
     * @param id
     * @return
     */
    @GetMapping("/delete")
    public @ResponseBody
    ResultBody delete(@RequestParam String id) {
        if (StringUtil.isEmpty(id)) {
            return new ResultBody(ResCodeEnum.UG_PAEAMES.getCode(), ResCodeEnum.UG_PAEAMES.getMsg());
        }
        boolean delete = userGroupService.delete(id);
        if (delete) {
            return new ResultBody(ResCodeEnum.SUCCESS.getCode(), ResCodeEnum.SUCCESS.getMsg());
        }
        return new ResultBody(ResCodeEnum.FAIL.getCode(), ResCodeEnum.FAIL.getMsg());
    }

    /**
     * 向分组中添加用户
     *
     * @param requestBody
     * @return
     */
    @PostMapping("/addUser")
    public @ResponseBody
    ResultBody addUser(@RequestBody UserGroupRequestBody requestBody) {
        boolean b = userGroupService.addUser(requestBody);
        if (b) {
            return new ResultBody(ResCodeEnum.SUCCESS.getCode(), ResCodeEnum.SUCCESS.getMsg());
        }
        return new ResultBody(ResCodeEnum.FAIL.getCode(), ResCodeEnum.FAIL.getMsg());
    }


    @PostMapping("/getUserByGroupId")
    public @ResponseBody
    ResultBody getUsers(@RequestBody UserGroupRequestBody requestBody) {
        try {
            List<String> userIdByGroupId = userGroupService.findUserIdByGroupId(requestBody);
            return new ResultBody(ResCodeEnum.SUCCESS.getCode(), ResCodeEnum.SUCCESS.getMsg(), userIdByGroupId);
        } catch (Exception e) {
            return new ResultBody(ResCodeEnum.FAIL.getCode(), ResCodeEnum.FAIL.getMsg(), e.getMessage());
        }
    }

    @PostMapping("/pushCoupon")
    public @ResponseBody
    ResultBody pushCoupon(@RequestBody UserGroupRequestBody requestBody) {
        try {
            boolean userIdByGroupId = userGroupService.pushCoupon(requestBody);
            if (!userIdByGroupId) {
                return new ResultBody(ResCodeEnum.FAIL.getCode(), ResCodeEnum.FAIL.getMsg());
            }
            return new ResultBody(ResCodeEnum.SUCCESS.getCode(), ResCodeEnum.SUCCESS.getMsg());
        } catch (Exception e) {
            return new ResultBody(ResCodeEnum.FAIL.getCode(), ResCodeEnum.FAIL.getMsg(), e.getMessage());
        }
    }

    @PostMapping("/table_list/{groupId}")
    @ResponseBody
    public Map<String, Object> tableList(@PathVariable String groupId, UserGroupRequestBody requestBody) {
        Integer page = requestBody.getPage();
        if (null != page && page > 0) {
            page = page - 1;
            requestBody.setPage(page);
        }
        requestBody.setGroupId(groupId);
        PageDTO<UserDTO> pageDTO = userGroupService.checkList(requestBody);
        Map<String, Object> data = new HashMap<>();
        if (null != pageDTO && pageDTO.getList()!= null) {
            data.put("size", pageDTO.getPageSize());
            data.put("rows", pageDTO.getList());
            data.put("status", 200);
            data.put("msg", "success!");
            data.put("total", pageDTO.getTotal());
        } else {
            data.put("size", 10);
            data.put("rows", null);
            data.put("status", -200);
            data.put("msg", "no data!");
            data.put("total", 0);
        }
        return data;
    }

    /**
     * 导出
     *
     * @return
     */
    @RequestMapping(value = "/export_user_group_record")
    @ResponseBody
    public ResultBody exportParkingRecord(@RequestParam String groupId) {
        UserGroupRequestBody requestBody = new UserGroupRequestBody();
        requestBody.setSize(99999);
        try {
            requestBody.setGroupId(groupId);
            userGroupService.exportUserGroup(this.response, requestBody);
            return new ResultBody(ResCodeEnum.SUCCESS.getCode(), ResCodeEnum.SUCCESS.getMsg());
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultBody(ResCodeEnum.FAIL.getCode(), ResCodeEnum.FAIL.getMsg());
        }
    }

}
